package com.ezmobo.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.PrintWriter;
import java.net.URL;
import java.util.List;

public class FileUtils {
	private static final String ROOT = "project/";

	@SuppressWarnings("resource")
	public static boolean isFileExist(String file) {
		try {
			new FileReader(file);
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	public static void copyFile(File file, String copy) throws Exception {
		BufferedReader reader = new BufferedReader(new FileReader(file));
		PrintWriter writer = new PrintWriter(copy, "UTF-8");
		String line;
		while ((line = reader.readLine()) != null) {
			writer.println(line);
		}
		writer.close();
		reader.close();
	}

	public static void createDirs(String path) throws Exception {
		File file = new File(path);
		file.mkdirs();
	}

	public static String getProjectDir() {
		String path = Thread.currentThread().getContextClassLoader().getResource("").toString();
		return path.substring("file:".length(), path.indexOf("target/"));
	}

	public static String getClassDir() {
		return getProjectDir() + "src/main/java/";
	}

	public static String getRootDir() {
		String path = Thread.currentThread().getContextClassLoader().getResource("").toString();
		return path.substring("file:".length(), path.indexOf(ROOT));
	}

	public static String getResourceDir(String file) {
		URL url = Thread.currentThread().getContextClassLoader().getResource(file);
		if (url != null) {
			String path = url.toString();
			return path.substring("file:".length(), path.indexOf(file));
		}
		return null;
	}

	public static String getResourcePath(String file) {
		URL url = Thread.currentThread().getContextClassLoader().getResource(file);
		if (url != null) {
			String path = url.toString();
			return path.substring("file:".length());
		}
		return null;
	}

	public static String getMainResourceDir() {
		return getProjectDir() + "src/main/resources/";
	}

	public static String getTestResourceDir() {
		return getProjectDir() + "src/test/resources/";
	}

	public static String getRelativeProjectDir() {
		return "";
	}

	public static String getRelativeClassDir() {
		return getRelativeProjectDir() + "src/main/java/";
	}

	public static String getRelativeMainResourceDir() {
		return getRelativeProjectDir() + "src/main/resources/";
	}

	public static String getRelativeTestResourceDir() {
		return getRelativeProjectDir() + "src/test/resources/";
	}

	public static void searchFile(File file, String pathContains, List<File> result) {
		if (file.isDirectory()) {
			File[] fileList = file.listFiles();
			if (fileList != null) {
				for (File sudFile : fileList) {
					searchFile(sudFile, pathContains, result);
				}
			}
		} else if (file.getAbsolutePath().contains(pathContains)) {
			result.add(file);
		}
	}

	public static String readFileContent(File file) throws Exception {
		StringBuffer buf = new StringBuffer();
		BufferedReader reader = new BufferedReader(new FileReader(file));
		String line;
		while ((line = reader.readLine()) != null) {
			buf.append(line);
		}
		reader.close();
		return buf.toString();
	}
}
